import { GTree } from 'geminis-react';
import { forwardRef, useImperativeHandle, useRef, useState } from 'react';

export const CagegoryTree = forwardRef((props: any, ref: any) => {
  const tree = useRef<any>();
  const [selection, setSelection] = useState<any>();

  useImperativeHandle(
    ref,
    () => {
      return {
        selection: tree.current.selection,
        reloadSelectedNode: () => tree.current.reloadSelectedNode(),
      };
    },
    [selection],
  );

  return (
    <GTree
      ref={tree}
      url="/api/everydata/categories/children/"
      root={props.showRoot ? { id: '', name: '数据类型', pathId: '/' } : null}
      onSelected={setSelection}
      {...props}
    />
  );
});
